package com.wye.controller;

import com.github.pagehelper.PageInfo;
import com.wye.goods.pojo.Brand;
import com.wye.goods.pojo.Category;
import com.wye.service.CategoryService;
import com.wye.service.IBrandService;
import entity.Result;
import entity.StatusCode;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/category")
@CrossOrigin
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping(value = "/list/{pid}")
    public Result findByParent(@PathVariable(value = "pid") Integer pid){
        List<Category> categoryList = categoryService.findByParentId(pid);
        return new Result(true, StatusCode.OK, "查询子节点成功", categoryList);
    }

    /**
     * 查询所有品牌
     */
    @GetMapping("/findAll")
    public Result<List<Brand>> findAll(){
        List<Category> categories = categoryService.findAll();
        return new Result<List<Brand>>(true, StatusCode.OK, "查询品牌集合", categories);
    }

    @GetMapping("/findCategoryById/{id}")
    public Result findCategoryById(@PathVariable(value = "id")Integer id){
        Category category = categoryService.findById(id);
        if (category == null){
            return new Result(false, StatusCode.ERROR, "请求失败");
        }
        return new Result(true, StatusCode.OK, "请求成功", category);
    }

    @PostMapping(value = "/add")
    public Result add(@RequestBody Category category){
        int resultInt = categoryService.insert(category);
        if (resultInt != 0){
            return new Result(true, StatusCode.OK, "添加成功");
        }
        return new Result(false, StatusCode.ERROR, "添加失败");
    }

    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public Result updateBrand(@RequestBody Category category){
        categoryService.update(category);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable(value = "id") Integer id){
        categoryService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @PostMapping(value = "/search")
    public Result<List<Category>> search(Category category){
        //调用service实现查询
        if (category != null){
            List<Category> categorys = categoryService.findList(category);
            return new Result<List<Category>>(true, StatusCode.OK, "查询成功", category);
        }
        return new Result<List<Category>>(false, StatusCode.ERROR, "请输入有效参数");
    }

    @RequestMapping(value = "/search/{page}/{size}")
    public Result findPage(@PathVariable(value = "page")Integer page, //当前页
                           @PathVariable(value = "size")Integer size,
                           Category category){//每页显示的条数
        if (category == null){
            PageInfo<Category> pageInfo = categoryService.findPage(page, size);
            return new Result(true, StatusCode.OK, "分页查询成功", pageInfo);
        }
        PageInfo<Category> pageInfo = categoryService.findPage(page, size, category);
        return new Result(true, StatusCode.OK, "分页搜索查询成功", pageInfo);

    }


}
